﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Library.DomainObject.SQLServer;
using System.Data;
using Dapper;

namespace Web.Library.DataAccess.SQLServer
{
    public partial class SysApplicationRepo
    {
        public SysAppGroupAuthorization RetrieveAuthorizationByGroupIDAndMenuID(int GroupAppID, int AppID)
        {
            SysAppGroupAuthorization t = null;
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                string strQuery = "SELECT AppAuthID, GroupAppID, AppID, AllowDisplay, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM dbo.SysAppGroupAuthorization with(nolock) WHERE GroupAppID = @GroupAppID AND AppID = @AppID ";
                try
                {
                    conn.Open();
                    t = conn.Query<SysAppGroupAuthorization>(strQuery, new { GroupAppID = GroupAppID, AppID = AppID }).SingleOrDefault();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }

            }
            return t;
        }


        public List<SysApplication> GetListByGroupAppID(string key)
        {
            List<SysApplication> tt = new List<SysApplication>();
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                string strQuery = @"SELECT B.AppID,B.TitleApp, B.DescApp, B.UrlApp, B.ImageApp, B.CreatedDate, B.CreatedBy, B.UpdatedDate, B.UpdatedBy 
                FROM SysAppGroupAuthorization A
                INNER JOIN SysApplication B ON A.AppID = B.AppID
                WHERE A.GroupAppID IN ("+key+") AND A.AllowDisplay=1";
                try
                {
                    conn.Open();
                    //tt = conn.Query<SysApplication>(strQuery, new { GroupAppID = key }).ToList<SysApplication>();

                    tt = conn.Query<SysApplication>(strQuery).ToList<SysApplication>();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }

            }
            return tt;
        }

        public bool UpdateWithoutImage(SysApplication domain)
        {
            int result = 0;
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                string sqlQuery = "UPDATE dbo.SysApplication SET TitleApp=@TitleApp,DescApp = @DescApp, UrlApp = @UrlApp, UpdatedDate = @UpdatedDate, UpdatedBy = @UpdatedBy WHERE AppID = @AppID";

                try
                {
                    conn.Open();
                    result = conn.Execute(sqlQuery, domain);
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }
            return (result > 0);
        }
    }
}
